package sg.edu.nus.iss.phoenix.user.controller;

import java.io.IOException;


import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sg.edu.nus.iss.phoenix.core.controller.FCUtilities;
import sg.edu.nus.iss.phoenix.user.delegate.UserDelegate;
import sg.edu.nus.iss.phoenix.user.entity.User;

/**
 * Servlet implementation class UserController
 * @author Dinh Ngoc Vu
 */
@WebServlet("/UserController/*")
public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}
	
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String selection = FCUtilities.stripPath(request.getPathInfo()).toLowerCase();
		int choice=0;
		if(selection.equalsIgnoreCase("loaduser"))
			choice = 1;
		if(selection.equalsIgnoreCase("setupuser"))
			choice = 2;
		if(selection.equalsIgnoreCase("deleteuser"))
			choice = 3;
		
		switch (choice) {		
		case 1:
			UserDelegate userdel1 = new UserDelegate();
			List<User> users = userdel1.getAllUser();
			request.setAttribute("users", users);
			RequestDispatcher rd1 = request.getRequestDispatcher("/pages/UserList.jsp");
			rd1.forward(request, response);
			break;
		case 2:
			UserDelegate userdel2 = new UserDelegate();
			User user = new User();
			String idIn = request.getParameter("id");
			String nameIn = request.getParameter("name");
			String passwordIn = request.getParameter("password");
			
			String[] roles = request.getParameterValues("roles");
			user.setAll(idIn, passwordIn, nameIn, roles[0]);
			
			String ins = (String) request.getParameter("ins");
			Logger.getLogger(getClass().getName()).log(Level.INFO,"Insert Flag: " + ins);
			if (ins.equalsIgnoreCase("true")) {
				try {
					userdel2.createUser(user);
					List<User> data = userdel2.getAllUser();
					request.setAttribute("users", data);
					RequestDispatcher rd2 = request.getRequestDispatcher("/pages/UserList.jsp");
					rd2.forward(request, response);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					request.setAttribute("error", "Cannot create an existing user");
					RequestDispatcher temp = request.getRequestDispatcher("/pages/error.jsp");
					temp.forward(request, response);
					//e.printStackTrace();
				}
			} else {
				userdel2.modifyUser(user);
				List<User> data = userdel2.getAllUser();
				request.setAttribute("users", data);
				RequestDispatcher rd2 = request.getRequestDispatcher("/pages/UserList.jsp");
				rd2.forward(request, response);
			}
			break;		
		case 3:
			UserDelegate userdel3 = new UserDelegate();
			String id = request.getParameter("id");
			User user1 = new User(id);
			try{
				userdel3.deleteUser(user1);
				List<User> data1 = userdel3.getAllUser();
				request.setAttribute("users", data1);
				RequestDispatcher rd3 = request.getRequestDispatcher("/pages/UserList.jsp");
				rd3.forward(request, response);
			}catch(SQLException e){
				request.setAttribute("error", "Cannot delete this user");
				RequestDispatcher temp = request.getRequestDispatcher("/pages/error.jsp");
				temp.forward(request, response);
			}
			break;
		default:			
			break;
		}
	}
	
}
